package com.cheyunkeji.er.adapter.fastevaluate;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.adapter.MyBaseAdapter;
import com.cheyunkeji.er.bean.fast_evaluate.NoticeBean;
import com.cheyunkeji.er.bean.fast_evaluate.TempNotificationListBean;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.http.RespCallback;
import com.cheyunkeji.er.view.SToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static android.content.ContentValues.TAG;

/** Created by Administrator on 2017/5/15. */
public class NoticeAdapter extends MyBaseAdapter {

  int pos = -1;

  //  公告消息来源数组
  private List<String> notifySourceType = null;

  public NoticeAdapter(ArrayList<NoticeBean> dataList, Context context) {
    super(dataList, context);
    //        getNotificationSourceType();
    notifySourceType = MyApplication.getInstance().getCarTempAttrList().getNm_from();
  }

  public void setPostion(int position) {
    this.pos = position;
    notifyDataSetChanged();
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
      convertView = LayoutInflater.from(context).inflate(R.layout.er_notice_item, parent, false);
      holder = new ViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    NoticeBean bean = (NoticeBean) dataList.get(position);
    int from = Integer.parseInt(bean.getFrom());
    //        holder.noticeItemTitle.setText((from==0?"[未知] ":from==1?"[通道] ":from==2?"[管理后台] ":"[系统
    // ]")+bean.getTitle());
    holder.noticeItemTitle.setText(
        (notifySourceType != null && notifySourceType.size() != 0)
            ? "[" + notifySourceType.get(from) + "]" + bean.getTitle()
            : "[未知]" + bean.getTitle());
    //        holder.noticeItemNumber.setText("总浏览：");
    holder.noticeItemTime.setText(bean.getDateline());

    if (pos != -1 && pos == position) {
      holder.noticeItemNew.setVisibility(View.GONE);
    } else {
      if (bean.getNo_read() == 0) {
        holder.noticeItemNew.setVisibility(View.GONE);
      } else {
        holder.noticeItemNew.setVisibility(View.VISIBLE);
      }
    }

    return convertView;
  }

  private void getNotificationSourceType() {

    HashMap<String, String> params = new HashMap<>();
    //      params.put("authkey",
    // UserInfoManager.getInstance(MyApplication.getInstance()).getAuthKey());//
    // params.put(ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getYkey(),
    // ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getY7e_e73a691f02a46500201c3998e6a2989d());
    params.put("field", "nm_from");

    ApiClient.postForm(
        HttpConstants.GET_TEMP_INFO_LIST,
        params,
        new RespCallback<TempNotificationListBean>() {

          @Override
          protected void onRequestFailed(String msg) {
            SToast.show(msg);
          }

          @Override
          protected void onSuccess(TempNotificationListBean data) {

            notifySourceType = data.getNm_from();
            NoticeAdapter.this.notifyDataSetChanged();

            Log.e(TAG, ": TempNotificationList : =============" + data.getNm_from().toString());
          }
        });
  }

  static class ViewHolder {
    @BindView(R.id.notice_item_title)
    TextView noticeItemTitle;

    @BindView(R.id.notice_item_new)
    ImageView noticeItemNew;

    @BindView(R.id.notice_item_time)
    TextView noticeItemTime;
    //        @BindView(R.id.notice_item_number)
    //        TextView noticeItemNumber;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }
}
